package com.bite.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class TimeRecordAspect {
//    @Around("com.bite.aop.aspect.AspectDemo.pt()")
    @Around("@annotation(com.bite.aop.aspect.TimeRecord)")
//    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object timeRecord(ProceedingJoinPoint pjt){
        //1. 记录开始时间
        //2. 执行目标代码
        //3. 记录结束时间
        //4. 返回结果
        log.info("TimeRecordAspect 前");
        long start = System.currentTimeMillis();

        //执行目标方法
        Object o = null;
        try {
            o = pjt.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        log.info("TimeRecordAspect 后");
        log.info(pjt.getSignature()+ "耗时: "+ (System.currentTimeMillis()-start) + "ms");
        return o;
    }
}
